import Vue from 'vue'
import App from './App.vue'
import router from './router'
import Navbar from "@/components/Navbar.vue"
import { isTokenEffective } from '@/api'

// 引入Element-UI
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

// 引入Font-Awesome
import 'font-awesome/css/font-awesome.min.css'

Vue.config.productionTip = false

//路由拦截，拦截全部路由，每次操作路由都是被拦截进⾏判断
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token");
  // console.log("token:", token)
  // 改进token判断逻辑，防止空字符串或无效值
  if (!token || token.trim() === '') {
    console.log("token is invalid or empty")
    // 添加路径判断，避免重复导航错误
    if (to.path !== '/login') {
      return next({ path: '/login' })
    } else {
      return next(); // 已经在登录页，无需跳转
    }
  } else {
    isTokenEffective({ token: token }).then((res) => {
      //筛选需要传token的路由，匹配route⾥⾯需要登录的路径，如果匹配到就是true
      if (to.matched.some((record) => record.meta.requiresAuth)) {
        //根据token是否有效，判断是否需要调到登录⻚⾯
        if (res.data.flag) {
          next()
        } else {
          return next({ path: '/login'})
        }
      } else {
        next();
      }
    })
  }
  
})

// 注册导航
Vue.component('Navbar', Navbar)

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
